<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@include file="../../util/taglib.jsp" %>
<!DOCTYPE html>
<html lang="en">
<head>
    <title>添加职位</title>
</head>
<body>
<div id="editAllpositionForm">
        <div class="form-Container">
            <form action="${pageContext.request.contextPath}/PositionController/editAllPosition.do"  id="dataForm" class="grayForm" method="post">
            <input type="hidden" name="curCompanyUUID" value="${param.companyUUID }" />
                <table>
                    <tr>
                        <td style="text-align: right;">
                            <input type="button" value="新增一栏"    id="createRow" />
                            <input type="button" value="移除选中栏" name="close" id="removeRow" style="background-color: #E56100"/>
                        </td>
                    </tr>
                </table>
                <table>
                   <tr>
                        <td ><label for="companyUUID">所属公司:</label></td>
                        <td style="line-height:20px;">
                          <input type="hidden" name="companyName"  id="companyName" value="" >
	                      <select name="companyUUID" id="companyUUID"  class="select-box" curV="" required>
	                         <option value="">请选择</option>
	                      </select>
                        </td>
                        <td ><label for="deptUUID">所属部门:</label></td>
                        <td style="line-height:20px;">
                             <input type="hidden" name="deptName"  id="deptName" value="" >
                             <select name="deptUUID" id="deptUUID"  class="select-box" curV="" required>
                                <option value="">请选择</option>
                             </select>
                        </td>
                    </tr>
                </table>
                <table id="contentTable">
                    <tr>
                        <td style="width:123px;"><label for="positionName">职位名称:</label></td>
                        <td style="line-height:20px;"><input type="text"  name="positionName" style="width:458px"/></td>
                    </tr>
                    <tr>
                        <td ><label for="positionName">职位名称:</label></td>
                        <td style="line-height:20px;"><input type="text"  name="positionName" style="width:458px"/></td>
                    </tr>
                    <tr>
                        <td ><label for="positionName">职位名称:</label></td>
                        <td style="line-height:20px;"><input type="text"  name="positionName" style="width:458px"/></td>
                    </tr>
                </table>
                <table>
                    <tr>
                        <td style="text-align: center;">
                            <input type="button" value="提交数据" name="submitData"  />
                            <input type="button" value="关闭" name="close" />
                        </td>
                    </tr>
                </table>
                <div id="confirmMsg"></div>
            </form>
        </div>
</div>
        <script type="text/javascript">
        var baseId = "#editAllpositionForm ";
        $(document).ready(function () {
        	var rules = {}
        	var messages = {}
        	messages = {
        		companyUUID:"请选择所属公司",
        		positionName:"请输入职位名字"
            };
        	
        	var validateForm = $(baseId+"#dataForm").validate({
                debug:true,
                rules:rules,
                messages:messages
            });
        	
        	//加载单位数据
        	loadCompanyList();
        	
        	//当改变选项后给companyName赋值，并出他的部门列表
        	$(baseId+"select[name='companyUUID']").change(function(){
        	    var companyName= $(baseId+"select[name='companyUUID'] option:selected").text();
        	    var companyUUID= $(baseId+"select[name='companyUUID'] option:selected").val();
        	    $(baseId+"input[name='companyName']").val(companyName)
        	    loadDeptList(companyUUID);
        	})
        	
        	//当改变选项后给companyName赋值，并出他的部门列表
        	$(baseId+"select[name='deptUUID']").change(function(){
        	    var deptName= $(baseId+"select[name='deptUUID'] option:selected").text();
        	    var deptUUID= $(baseId+"select[name='deptUUID'] option:selected").val();
        	    $(baseId+"input[name='deptName']").val(deptName);
        	})
        	
        	//表格新增一行
        	$(baseId+" #createRow").click(function(){
        		var firstTrDom = $(baseId+" #contentTable tr:first");
        		var trLength = $(baseId+" #contentTable tr").length+1;
        		var rowHtml = firstTrDom.html();
        		rowHtml = "<tr>"+rowHtml+"</tr>";
        		$(baseId+" #contentTable").append(rowHtml);
        	})
        	
        	//表格移除
        	$(baseId+" #removeRow").click(function(){
        		var trLength = $(baseId+" #contentTable tr").length
        		var checkedLength = $(baseId+" #contentTable input:checked").length;
        		if(trLength>checkedLength){
        			$(baseId+" #contentTable :checkbox").each(function(i){
            			if(checked=="checked"){
            				$(this).parent().parent().remove();
            			}
            		})
        		}else{
        			$.messager.alert('我的消息','不可全部移除,至少保留一行！','info');
        		}
        	})
        	
            //提交数据到服务器
            $(baseId+" input[name='submitData']").click(function () {
            	//提交表单进行验证
                var actionUrl = $(baseId+" #dataForm").attr("action");
            	var positionList = [];
            	var companyUUID = $(baseId+" select[name='companyUUID']").val();
            	var companyName = $(baseId+" input[name='companyName']").val();
            	var deptName = $(baseId+" input[name='deptName']").val();
            	var deptUUID = $(baseId+" select[name='deptUUID']").val();
            	
            	$(baseId+" input[name='positionName']").each(function(i){
            		if($(this).val()!=""){
            			var position = new Object();
                		position.companyUUID = companyUUID;
                		position.companyName = companyName;
                		position.deptUUID = deptUUID;
                		position.deptName = deptName;
                		position.positionName = $(this).val();
                		position.positionDesc = position.positionName;
                		positionList.push(position);
            		}
            	})
            	var formData = $.toJSON(positionList);
            	
            	//提交表单进行验证
            	$("#dataForm").submit();
                var validateFlag = validateForm.form();
                if(validateFlag){
                	$.post(actionUrl,{formData:formData},function(data){
                		if(data.code=="00"){
            				  $.messager.confirm('操作确认',data.msg+',请确认是否继续操作？', function(flag){
            					  if(flag){
            						  $("#editDataFormArea").window("close");
            						  tableObject.datagrid('reload');
            					  }
            				  });
            			    }else{
            				   $.messager.alert('操作提示',data.message,'error');
            			    }
          		    },"json");
                }
            });
        });
        
        //加载单位数据
        function loadCompanyList(){
        	$.post(getBasePath()+"company/getJsonAllList.do",{},function(jsonData){
        		var optionList = "<option value=\"\">请选择</option>";
        		var selectArea = $(baseId+" select[name='companyUUID']");
        		var curUUID = $(baseId+" input[name='curCompanyUUID']").val();
        		$.each(jsonData.data,function(i,item){
        			if(item.uuid==curUUID){
        				optionList+= "<option value=\""+item.uuid+"\" selected>"+item.companyName+"</option>";
        			}else{
        				optionList+= "<option value=\""+item.uuid+"\">"+item.companyName+"</option>";
        			}
        		})
        		selectArea.html(optionList);
  		    },"json");
        }
        
        //加载部门数据
        function loadDeptList(companyUUID){
        	$.post(getBasePath()+"department/getJsonAllList.do",{companyUUID:companyUUID},function(jsonData){
        		var optionList = "<option value=\"\">请选择</option>";
        		$.each(jsonData.data,function(i,item){
        			optionList+= "<option value=\""+item.uuid+"\">"+item.departmentName+"</option>";
        		})
        		$(baseId+"select[name='deptUUID']").html(optionList);
  		    },"json");
        }
    </script>
</body>
</html>